package com.dishdex.menuhub.app.util;

import com.dishdex.menuhub.app.domain.dish.DishExportVO;
import com.dishdex.menuhub.app.domain.dish.DishImageOfDishItemVO;
import com.dishdex.menuhub.app.domain.dish.DishItemVO;
import com.dishdex.menuhub.module.module.category.entity.Category;
import com.dishdex.menuhub.module.module.dish.entity.Dish;
import com.dishdex.menuhub.module.module.category.service.CategoryService;
import com.dishdex.menuhub.module.utils.BaseUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * ClassName:
 * Package: com.dishdex.menuhub.app.util
 * Description:
 *
 * @Author:
 * @Create 2025/8/29 17:52
 * @Version 1.0
 */
public final class DishConverter {

    private DishConverter() {
    }   // 禁止实例化

    public static DishItemVO toDishItemVO(Dish d, CategoryService categoryService) {
        DishItemVO vo = new DishItemVO()
                .setDishId(d.getId() == null ? -1 : d.getId())
                .setDishImage(new DishImageOfDishItemVO(d.getCoverImages().split("\\$")[0], BaseUtils.splitByAr(d.getCoverImages().split("\\$")[0])))
                .setDishName(d.getDishName() == null ? "" : d.getDishName())
                .setDishPrice(d.getDishPrice() == null ? -1 : d.getDishPrice())
                .setDishScore(d.getDishScore() == null ? -1 : d.getDishScore());
        try {
            Category c = categoryService.getCategory(d.getCategoryId());
            vo.setCategoryName(c.getCategoryName() == null ? "" : c.getCategoryName());
        } catch (RuntimeException e) {
            vo.setCategoryName(e.getMessage());
        }
        return vo;
    }

    public static List<DishExportVO> toDishExportVO(List<Dish> dishList) {
        List<DishExportVO> dishExportVOList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (Dish d : dishList) {
            DishExportVO vo = new DishExportVO();
            vo.setDishName(d.getDishName() == null ? "" : d.getDishName());
            vo.setDishDescription(d.getDishDescription() == null ? "" : d.getDishDescription());
            vo.setDishScore(d.getDishScore() == null ? -1 : d.getDishScore());
            vo.setDishPrice(d.getDishPrice());
            vo.setDishIngredients(d.getDishIngredients() == null ? "" : d.getDishIngredients());
            vo.setDishReview(d.getDishReview() == null ? "" : d.getDishReview());
            vo.setCoverImages(d.getCoverImages() == null ? "" : d.getCoverImages());
            vo.setCreateTime(d.getCreateTime() == null ? "" : sdf.format(new Date(d.getCreateTime().longValue() * 1000L)));
            vo.setUpdateTime(d.getUpdateTime() == null ? "" : sdf.format(new Date(d.getUpdateTime().longValue() * 1000L)));
            vo.setIs_deleted(d.getIsDeleted() == null ? -1 : d.getIsDeleted());
            vo.setCategoryId(d.getCategoryId() == null ? 0 : d.getCategoryId());
            dishExportVOList.add(vo);
        }
        return dishExportVOList;
    }
}
